package com.duyj.base;

import org.apache.commons.collections4.MapUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * 上下文工具类
 */
@Component
@Lazy(value = false) // ApplicationContextUtils 禁止懒加载，否则会出现空指针异常
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) {
        ApplicationContextUtils.context = context;
    }

    /**
     * 获得指定类型的对象
     *
     * @param clazz 类型
     * @return T
     */
    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }

    /**
     * 获得指定bean名称的对象
     *
     * @param beanName bean名称，类名首字母小写
     * @return Object
     */
    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    /**
     * 获得指定bean类型和名称的对象
     *
     * @param clazz    类型
     * @param beanName bean名称，类名首字母小写
     * @return T
     */
    public static <T> T getBean(Class<T> clazz, String beanName) {
        Map<String, T> clazzMap = ApplicationContextUtils.getBeansOfType(clazz);
        if (MapUtils.isEmpty(clazzMap)) {
            return null;
        }
        return clazzMap.get(beanName);
    }

    /**
     * 获得指定类型的实现类
     *
     * @param clazz 类型
     * @return Key:类名，Value 具体类
     */
    public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
        return context.getBeansOfType(clazz);
    }


    /**
     * 扫描指定包名
     *
     * @param basePackage 包名
     * @return 返回类路径数组
     * @throws IOException 异常
     */
    private static Resource[] doScan(String basePackage) throws IOException {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                + ClassUtils.convertClassNameToResourcePath(basePackage) + "/**/*.class";
        return resourcePatternResolver.getResources(pattern);
    }

    /**
     * 获得指定注解类型的类
     *
     * @param annotationType 注解
     * @return Key:类名，Value 具体类
     */
    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
        return context.getBeansWithAnnotation(annotationType);
    }

    /**
     * 发布事件
     *
     * @param event 事件类型
     */
    public static void publishEvent(ApplicationEvent event) {
        context.publishEvent(event);
    }

}
